# Copyright (C) 2025 Huawei Technologies Co., Ltd. All rights reserved.	

<# NOTE: openUBMC Systems module Cmdlets #>

function Get-openUBMCSystemInfo {
<#
.SYNOPSIS
Get system resource details of the server.

.DESCRIPTION
Get system resource details of the server.

.PARAMETER Session
openUBMC redfish session object which is created by Connect-openUBMC cmdlet.
A session object identifies an openUBMC server to which this cmdlet will be executed.

.OUTPUTS
String
Returns openUBMC Asset Tag if cmdlet executes successfully.
In case of an error or warning, exception will be returned.

.EXAMPLE

PS C:\> $credential = Get-Credential
PS C:\> $session = Connect-openUBMC -Address 192.168.1.1 -Credential $credential -TrustCert
PS C:\> $System = Get-openUBMCSystemInfo $session
PS C:\> $System

Host             : 192.168.1.1
Id               : 1
Name             : Computer System
AssetTag         : my test
Manufacturer     :
Model            : 2288H V5
SerialNumber     : 2102311TYBN0J3000293
UUID             : 877AA970-58F9-8432-E811-80345C184638
HostName         :
PartNumber       : 02311TYB
HostingRole      : {ApplicationServer}
Status           : @{State=Disabled; Health=OK}
PowerState       : Off
Boot             : @{BootSourceOverrideTarget=Pxe; BootSourceOverrideEnabled=Continuous; BootSourceOverrideMode=Legacy; BootSourceOverride
                    Target@Redfish.AllowableValues=System.Object[]}
TrustedModules   :
BiosVersion      : 0.81
ProcessorSummary : @{Count=2; Model=Central Processor; Status=}
MemorySummary    : @{TotalSystemMemoryGiB=128; Status=}
PCIeDevices      : {}
PCIeFunctions    : {}
Oem              : @{openUBMC=}

PS C:\> $System.Boot | fl

BootSourceOverrideTarget                         : Pxe
BootSourceOverrideEnabled                        : Continuous
BootSourceOverrideMode                           : Legacy
BootSourceOverrideTarget@Redfish.AllowableValues : {None, Pxe, Floppy, Cd...}


.LINK
https://gitcode.com/openUBMC/cmdlets_plugin

Connect-openUBMC
Disconnect-openUBMC
#>
  [CmdletBinding()]
  param (
    [RedfishSession[]]
    [parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, Position = 0)]
    $Session
  )

  begin {
  }

  process {
    Assert-ArrayNotNull $Session 'Session'

    $Logger.info("Invoke Get openUBMC System function")

    $ScriptBlock = {
      param($RedfishSession)
      $(Get-Logger).info($(Trace-Session $RedfishSession "Invoke Get openUBMC System now"))
      $Path = "/Systems/$($RedfishSession.Id)"
      $Response = Invoke-RedfishRequest $RedfishSession $Path | ConvertFrom-WebResponse
      $Properties = @(
        "Id", "Name", "AssetTag", "Manufacturer", "Model", "SerialNumber", "UUID",
        "HostName", "PartNumber", "HostingRole", "Status", "PowerState", "Boot", "TrustedModules",
        "BiosVersion", "ProcessorSummary", "MemorySummary", "PCIeDevices", "PCIeFunctions",
        "Oem"
      )

      $System = Copy-ObjectProperties $Response $Properties

      $Excludes = @(
        "InfiniBandInterfaces", "NetworkBondings", "ProcessorView",
        "MemoryView", "ProcessorsHistoryUsageRate", "MemoryHistoryUsageRate",
        "NetworkHistoryUsageRate"
      )
      $Oem = Copy-ObjectExcludes $Response.Oem.openUBMC $Excludes
      $System.Oem.openUBMC = $Oem
      return Update-SessionAddress $RedfishSession $System
    }

    try {
      $tasks = New-Object System.Collections.ArrayList
      $pool = New-RunspacePool $Session.Count
      for ($idx = 0; $idx -lt $Session.Count; $idx++) {
        $RedfishSession = $Session[$idx]
        $Logger.info($(Trace-Session $RedfishSession "Submit Get openUBMC System task"))
        [Void] $tasks.Add($(Start-ScriptBlockThread $pool $ScriptBlock @($RedfishSession)))
      }

      $Results = Get-AsyncTaskResults $tasks
      return ,$Results
    }
    finally {
      Close-Pool $pool
    }
  }

  end {
  }
}


function Get-openUBMCSystemNetworkSettings {
<#
.SYNOPSIS
Get system resource details of the server.

.DESCRIPTION
Get system resource details of the server. Server OS system and iBMA should be installed to support this cmdlet.

.PARAMETER Session
openUBMC redfish session object which is created by Connect-openUBMC cmdlet.
A session object identifies an openUBMC server to which this cmdlet will be executed.

.OUTPUTS
PSObject[][]
Returns openUBMC System LinkUp Ethernet Interfaces if cmdlet executes successfully.
In case of an error or warning, exception will be returned.

.EXAMPLE

PS C:\> $credential = Get-Credential
PS C:\> $session = Connect-openUBMC -Address 192.168.1.1-5 -Credential $credential -TrustCert
PS C:\> $Interfaces = Get-openUBMCSystemNetworkSettings $session
PS C:\> $Interfaces

Host                : 192.168.1.1
Id                  : mainboardLOMPort1
Name                : System Ethernet Interface
PermanentMACAddress : xx:xx:xx:xx:xx:xx
MACAddress          : xx:xx:xx:xx:xx:xx
LinkStatus          : LinkUp
IPv4Addresses       : {@{Address=192.168.1.10; SubnetMask=255.255.0.0; Gateway=10.1.0.1; AddressOrigin=}}
IPv6Addresses       : {@{Address=fe80::20; PrefixLength=10; AddressOrigin=SLAAC; AddressState=},
                      @{Address=fe80::21;PrefixLength=10; AddressOrigin=SLAAC; AddressState=},
                      @{Address=fe80::22; PrefixLength=10; AddressOrigin=Static; AddressState=}}
IPv6DefaultGateway  : fe80::1
InterfaceType       : Physical
BandwidthUsage      : 0
BDF                 : 0000:1a:00.0

Host                : 192.168.1.3
Id                  : mainboardLOMPort2
Name                : System Ethernet Interface
PermanentMACAddress : xx:xx:xx:xx:xx:xx
MACAddress          : xx:xx:xx:xx:xx:xx
LinkStatus          : NoLink
IPv4Addresses       : {}
IPv6Addresses       : {}
IPv6DefaultGateway  :
InterfaceType       : Physical
BandwidthUsage      :
BDF                 : 0000:1a:00.1

Host                : 192.168.1.4
Id                  : mainboardLOMPort3
Name                : System Ethernet Interface
PermanentMACAddress : xx:xx:xx:xx:xx:xx
MACAddress          : xx:xx:xx:xx:xx:xx
LinkStatus          :
IPv4Addresses       : {}
IPv6Addresses       : {}
IPv6DefaultGateway  :
InterfaceType       : Physical
BandwidthUsage      :
BDF                 : 0000:1a:00.2

Host                : 192.168.1.5
Id                  : mainboardLOMPort4
Name                : System Ethernet Interface
PermanentMACAddress : xx:xx:xx:xx:xx:xx
MACAddress          : xx:xx:xx:xx:xx:xx
LinkStatus          : NoLink
IPv4Addresses       : {}
IPv6Addresses       : {}
IPv6DefaultGateway  :
InterfaceType       : Physical
BandwidthUsage      :
BDF                 : 0000:1a:00.3

.LINK
https://gitcode.com/openUBMC/cmdlets_plugin

Connect-openUBMC
Disconnect-openUBMC
#>
  [CmdletBinding()]
  param (
    [RedfishSession[]]
    [parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, Position = 0)]
    $Session
  )

  begin {
  }

  process {
    Assert-ArrayNotNull $Session 'Session'

    $Logger.info("Invoke Get openUBMC System Networking Settings function")

    $ScriptBlock = {
      param($RedfishSession)
      $(Get-Logger).info($(Trace-Session $RedfishSession "Invoke Get openUBMC System Networking Settings now"))
      $GetInterfacesPath = "/Systems/$($RedfishSession.Id)/EthernetInterfaces"
      $EthernetInterfaces = Invoke-RedfishRequest $RedfishSession $GetInterfacesPath | ConvertFrom-WebResponse
      $Results = New-Object System.Collections.ArrayList
      for ($idx=0; $idx -lt $EthernetInterfaces.Members.Count; $idx++) {
        $Member = $EthernetInterfaces.Members[$idx]
        $EthernetInterface = Invoke-RedfishRequest $RedfishSession $Member.'@odata.id' | ConvertFrom-WebResponse
        # $Logger.Debug($(Trace-Session $RedfishSession "Load EthernetInterface: $EthernetInterface"))
        # if ($BMC.LinkStatus.LinkUp -eq $EthernetInterface.LinkStatus) {
        $Properties = @(
          "Id", "Name", "PermanentMACAddress", "MACAddress", "LinkStatus",
          "IPv4Addresses", "IPv6Addresses", "IPv6DefaultGateway"
        )
        $Clone = Copy-ObjectProperties $EthernetInterface $Properties
        $Clone | Add-Member -MemberType NoteProperty "InterfaceType" $EthernetInterface.Oem.openUBMC.InterfaceType
        $Clone | Add-Member -MemberType NoteProperty "BandwidthUsage" $EthernetInterface.Oem.openUBMC.BandwidthUsage
        $Clone | Add-Member -MemberType NoteProperty "BDF" $EthernetInterface.Oem.openUBMC.BDF
        $Clone = Update-SessionAddress $RedfishSession $Clone
        [Void] $Results.add($Clone)
        # }
      }

      if ($Results.Count -eq 0) {
        throw $(Get-i18n FAIL_NO_LINKUP_INTERFACE)
      }

      return ,$Results.ToArray()
    }

    try {
      $tasks = New-Object System.Collections.ArrayList
      $pool = New-RunspacePool $Session.Count
      for ($idx = 0; $idx -lt $Session.Count; $idx++) {
        $RedfishSession = $Session[$idx]
        $Logger.info($(Trace-Session $RedfishSession "Submit Get openUBMC System Networking Settings task"))
        [Void] $tasks.Add($(Start-ScriptBlockThread $pool $ScriptBlock @($RedfishSession)))
      }
      $Results = Get-AsyncTaskResults $tasks
      return ,$Results
    }
    finally {
      Close-Pool $pool
    }
  }

  end {
  }
}
